package com.xiaoma.tpo.webservice;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xmlbeans.impl.jam.mutable.MPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoma.tpo.domain.AdPosition;
import com.xiaoma.tpo.domain.VPhysicalAddress;
import com.xiaoma.tpo.domain.VVersion;
import com.xiaoma.tpo.dto.AdCountVO;
import com.xiaoma.tpo.dto.AdVo;
import com.xiaoma.tpo.dto.PhyVersionVO;
import com.xiaoma.tpo.dto.SelectBoxVO;
import com.xiaoma.tpo.service.AdService;
import com.xiaoma.tpo.utils.Pager;

@Controller
@RequestMapping("/service")
public class AdAndVersionWS {
	 @Autowired
	 private AdService adService;
	 
	 
	 /**
	  *
	  *   广告
	  * 
	  */
	 //广告管理
    @RequestMapping(value = "/ad/list" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> adList(String day, String sStatus,String sPosition,String sName,Integer page,Integer rows){
    	Map<String,Object> map = new HashMap<String, Object>();
    	Pager pager = adService.listAd(day,sStatus,sPosition,sName,page,rows);
    	map.put("rows", pager.getList());
    	map.put("total", pager.getTotalCount());
    	return map;
    }
    //添加广告
    @RequestMapping(value = "/ad/add" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> adAdd(@RequestParam MultipartFile file,@ModelAttribute AdVo adVo){
    	Map<String,Object> map = new HashMap<String, Object>();
    	//先做判断是否有正在活动的广告
    	if(!"0".equals(adVo.getStatus())){
    		boolean judge = adService.judgeActiveAd(adVo);
    		if(judge == true){
    			map.put("status", "-1");
    			return map;
    		}
    	}
    	boolean b = adService.addAd(file,adVo);
    	map.put("status", b);
    	return map;
    }
    //修改广告
    @RequestMapping(value = "/ad/edit" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> adEdit(@RequestParam MultipartFile file,@ModelAttribute AdVo adVo){
    	Map<String,Object> map = new HashMap<String, Object>();
    	//先做判断是否有正在活动的广告
    	if(!"0".equals(adVo.getStatus())){
    		boolean judge = adService.judgeActiveAd(adVo);
    		if(judge == true){
    			map.put("status", "-1");
    			return map;
    		}
    	}
    	boolean b = adService.editAd(file,adVo);
    	map.put("status", b);
    	return map;
    }
    //删除广告
    @RequestMapping(value = "/ad/remove" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> adRemove(Integer id){
    	Map<String,Object> map = new HashMap<String, Object>();
    	
    	boolean b = adService.removeAd(id);
    	map.put("status", b);
    	return map;
    }
    //获取广告位下拉菜单
    @RequestMapping(value = "/combo/ad",method = {RequestMethod.GET})
    @ResponseBody
    public List<SelectBoxVO> getComboAd(){
    	List<SelectBoxVO> sbvo = adService.getComboAd();
    	return sbvo;
    }
    //获取平台下拉菜单
    @RequestMapping(value = "/combo/platform",method = {RequestMethod.GET})
    @ResponseBody
    public List<SelectBoxVO> getComboPlatform(){
    	List<SelectBoxVO> sbvo = adService.getComboPlatform();
    	return sbvo;
    }
    //ckeditor上传图片
    @RequestMapping(value="/upload/image",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String uploadImage(@RequestParam MultipartFile upload,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
    	request.setCharacterEncoding("utf-8");
    	response.setContentType("text/html; charset=utf-8");
    	String[] type = upload.getContentType().split("/");
    	if(!"jpg".equals(type[1]) && !"gif".equals(type[1]) && !"bmp".equals(type[1]) && !"png".equals(type[1]) && !"jpeg".equals(type[1])){
    		return "<font color=\"red\"size=\"2\">* The type must be （.jpg/.gif/.bmp/.png/.jpeg）</font>";
    	}//.jpg/.gif/.bmp/.png/.jpeg文件
    	String url = adService.uploadImage(upload);
    	
    	String callback =request.getParameter("CKEditorFuncNum"); 
    	String rString= "  <script type=\"text/javascript\">"               
                      + "  window.parent.CKEDITOR.tools.callFunction(" + callback  
                      + "  ,'" + url + "','')"// 相对路径用于显示图片  
                      + "   </script>";  
    	return rString;
    }
    /**
     * 
     *   查询位置
     * 
     */
    //查询位置
    @RequestMapping(value = "/ad/list/position" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<AdPosition> adListPosition(){
    	List<AdPosition> adPositions = adService.getAllPosition();
    	return adPositions;
    }
    //添加位置
    @RequestMapping(value = "/ad/add/position" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> adAddPosition(@ModelAttribute AdPosition adPosition){
    	Map<String,Object> map = new HashMap<String, Object>();
    	boolean b = adService.addPosition(adPosition);
    	map.put("status", b);
    	return map;
    }
    //修改位置
    @RequestMapping(value = "/ad/edit/position" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> adEditPosition(@ModelAttribute AdVo adVo){
    	Map<String,Object> map = new HashMap<String, Object>();
    	
    	boolean b = adService.editAdPosition(adVo);
    	map.put("status", b);
    	return map;
    }
    //删除位置
    @RequestMapping(value = "/ad/delete/position" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> adDeletePosition(Integer id){
    	Map<String,Object> map = new HashMap<String, Object>();
    	boolean b = adService.removeAdPosition(id);
    	map.put("status", b);
    	return map;
    }
    /**
     * 
     *    平台管理
     * 
     */
    //查询物理地址
    @RequestMapping(value = "/ad/list/paddress" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<VPhysicalAddress> adListPAddress(){
    	List<VPhysicalAddress> rVPhysicalAddress = adService.getAllPAddress();
    	return rVPhysicalAddress;
    }
  //添加物理地址
    @RequestMapping(value = "/ad/add/paddress" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> adAddPAddress(@ModelAttribute VPhysicalAddress vPhysicalAddress){
    	Map<String,Object> map = new HashMap<String, Object>();
    	boolean b = adService.addPAddress(vPhysicalAddress);
    	map.put("status", b);
    	return map;
    }
    //修改物理地址
    @RequestMapping(value = "/ad/edit/paddress" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> adEditPAddress(@ModelAttribute VPhysicalAddress vPhysicalAddress){
    	Map<String,Object> map = new HashMap<String, Object>();
    	
    	boolean b = adService.editPAddress(vPhysicalAddress);
    	map.put("status", b);
    	return map;
    }
    //删除物理地址
    @RequestMapping(value = "/ad/delete/paddress" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> adDeletePAddress(Integer id){
    	Map<String,Object> map = new HashMap<String, Object>();
    	boolean b = adService.removePAddress(id);
    	map.put("status", b);
    	return map;
    }
    
    
    
    /**
     * 
     *   版本控制 
     * 
     */
    //版本控制
    @RequestMapping(value = "/version/list" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> versionList(@RequestParam String day,Integer page,Integer rows){
    	Map<String,Object> map = new HashMap<String, Object>();
    	Pager pager = adService.listVersion(day,page,rows);
    	map.put("rows", pager.getList());
    	map.put("total", pager.getTotalCount());
    	return map;
    }
    //添加版本
    @RequestMapping(value = "/version/add" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> versionAdd(@ModelAttribute VVersion vVersion){
    	Map<String,Object> map = new HashMap<String, Object>();
    	//避免重复的验证
    	if(adService.validate(vVersion)){
    		map.put("status", -1);
    		map.put("message", "VERSION 已存在");
    		return map;
    	}
    	if(adService.addVersion(vVersion)){
    		map.put("status", 1);
    		map.put("status", "添加成功");
    	}else{
    		map.put("status", -2);
    		map.put("status", "添加失败");
    	}
    	return map;
    }
    //修改版本
    @RequestMapping(value = "/version/edit" ,method = {RequestMethod.POST})
    @ResponseBody
    public Map<String,Object> versionEdit(@ModelAttribute VVersion vVersion){
    	Map<String,Object> map = new HashMap<String, Object>();
    	//避免重复的验证
    	if(adService.validate(vVersion)){
    		map.put("status", -1);
    		map.put("message", "VERSION 已存在");
    		return map;
    	}
    	boolean b = adService.editVersion(vVersion);
    	map.put("status", b);
    	return map;
    }
    //删除版本
    @RequestMapping(value = "/version/remove" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> versionRemove(Integer id){
    	Map<String,Object> map = new HashMap<String, Object>();
    	
    	boolean b = adService.removeVersion(id);
    	map.put("status", b);
    	return map;
    }
    //强制更新为最新版本
    @RequestMapping(value = "/version/forceUpdate" ,method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> versionForceUpdate(@RequestParam Integer id,@RequestParam String guid){
    	Map<String, String> result = new HashMap<String,String>();
     	Integer b = adService.forceUpdate(id,guid);
     	result.put("status", b.toString());
    	return result;
    }
    
    //统计广告
    @RequestMapping(value = "/ad/count" ,method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> adCount(Integer adId,Integer positionId,String platformGuid,Integer page,Integer rows){
    	Map<String,Object> map = new HashMap<String, Object>();
    	
    	Pager pager = adService.adCount(adId,positionId,platformGuid,page,rows);
    	map.put("rows", pager.getList());
    	map.put("total", pager.getTotalCount());
    	return map;
    }
   //获取平台或者广告位的下拉菜单
    @RequestMapping(value = "/combo/class",method = {RequestMethod.GET})
    @ResponseBody
    public List<SelectBoxVO> getComboClass(){
    	List<SelectBoxVO> sbvo = adService.getComboClass();
    	return sbvo;
    }
    
}
